<#-- 导入模版定义文件-->
<#include "../template/template_editPage.ftl">
<@edit title="商品信息管理">
    <#if (trader.traderId)??>
        <#assign isEdit = true />
    <#else>
        <#assign isAdd = true />
    </#if>
<body class="input">

<script type="text/javascript">
    $().ready(function () {

        var $validateErrorContainer = $("#validateErrorContainer");
        var $validateErrorLabelContainer = $("#validateErrorContainer ul");
        var $validateForm = $("#inputForm");
        var $tab = $("#tab");

        // Tab效果
        $tab.tabs(".tabContent", {
            tabs:"input"
        });

        // 表单验证
        $validateForm.validate({
            errorContainer:$validateErrorContainer,
            errorLabelContainer:$validateErrorLabelContainer,
            wrapper:"li",
            errorClass:"validateError",
            ignoreTitle:true,
            rules:{
                "operator.operatorName":{
                    required:true
                },
                "operator.operatorPassword":{
                    required:true
                },
                "account.payPassword":{
                    required:true
                },
                "operator.email":{
                    required:true
                },
                "trader.traderName":{
                    required:true
                },
                "trader.legelName":{
                    required:true
                },
                "trader.legelCard":{
                    required:true
                },
                "trader.mobile":{
                    required:true
                }
            },
            messages:{
                "operator.operatorName":{
                    required:"登陆名不能为空！"
                },
                "operator.operatorPassword":{
                    required:"密码不能为空！"
                },
                "account.payPassword":{
                    required:"交易密码不能为空！"
                },
                "operator.email":{
                    required:"邮箱不能为空！"
                },
                "trader.traderName":{
                    required:"单位名称不能为空！"
                },
                "trader.legelName":{
                    required:"负责人不能为空！"
                },
                "trader.legelCard":{
                    required:"身份证不能为空！"
                },
                "trader.mobile":{
                    required:"手机号不能为空！"
                }
            },
            submitHandler:function (form) {
                //$(form).find(":submit").attr("disabled", true);
                form.submit();
            }
        });
        <#if actionErrors?? && (actionErrors?size > 0)>
            $.messager.show("提示信息", "<#list errorMessages as list>${list}<br></#list>", 2000);
        </#if>
    });
    var res = true ;
    var checkuse = true ;
    var checktrader = true ;
    function checkPrice() {
        $("#priceSpan").html('');
        var e = /^(([1-9]\d*)|\d)(\.\d{1,3})?$/;
        var blsx = $("#commonSalePrice").val();
        if (!e.test(blsx)) {
            $("#priceSpan").html('价格格式不正确！(例：0.000)');
            return false;
        }
        else {
            return true;
        }
    }
	function checkUserName(){
		if($.trim($('#username').val()) != ''){
			$.post(
					'${base}/ekhui/trader!checkUserName.htm',
					{"operator.operatorName":$('#username').val()},
					function(data){
						if(data == 'true'){
							alert('该登陆名已被使用!') ;
							res = false ;
							checkuse = false ;
							return ;
						}
					}
				);
		}
	}
	function checkTraderName(){
		if($.trim($('#traderName').val()) != ''){
			$.post(
					'${base}/ekhui/trader!checkTraderName.htm',
					{"trader.traderName":$('#traderName').val()},
					function(data){
						if(data == 'true'){
							alert('该单位名称已被使用!') ;
							res = false ;
							checktrader = false ;
							return ;
						}
					}
				);
		}
	}
    function checkParvalue() {
        $("#priceSpan2").html('');
        var e = /^(([1-9]\d*)|\d)(\.\d{1,3})?$/;
        var blsx = $("#goodsMoney").val();
        if (!e.test(blsx)) {
            $("#priceSpan2").html('价格格式不正确！(例：0.000)');
            return false;
        }
        else {
            return true;
        }
    }
function getCitys(){
	var provinceId = $('#provinceId').val();
	$.getJSON(
		'${base}/ekhui/trader!getCitys.htm',
		{'province.provinceId':provinceId},
		function(data){
			if(document.getElementById('cityId') != null){
				$('#cityId').remove();
			}
			var citySelect = $("<select  onchange=\"getRegions();\">");
			$(citySelect).attr("id","cityId");
			$(citySelect).attr("name","trader.cityId");
			$(citySelect)[0].options.add(new Option("请选择市", "0"));  
			$.each(data,function(k,v){
				var option =  $("<option value="+data[k].cityId+">"+data[k].cityName+"</option>");
				$(citySelect).append($(option));
			});
			$('#cityDiv').append($(citySelect));
		}
	);
}
function getRegions(){
	var cityId = $('#cityId').val();
	$.getJSON(
			'${base}/ekhui/trader!getArea.htm',
		{'city.cityId':cityId},
		function(data){
			// alert(data) ;
			if(document.getElementById('areaId') != null){
				$('#areaId').remove();
			}
			var areaSelect = $("<select>");
			$(areaSelect).attr("id","areaId");
			$(areaSelect).attr("name","trader.areaId");
			$(areaSelect)[0].options.add(new Option("请选择区", "0"));  
			$.each(data,function(k,v){
				var option =  $("<option value="+data[k].areaId+">"+data[k].areaName+"</option>");
				$(areaSelect).append($(option));
			});
			$('#areaDiv').append($(areaSelect));
		}
	);
}
function addTrader(){
	
	if($.trim($('#username').val()) == ''){
		alert('登陆名不能为空！') ;
		res = false ;
		return ;
	}
	
	if($.trim($('#password').val()) == ''){
		alert('密码不能为空！') ;
		res = false ;
		return ;
	}
	if($.trim($('#passwordt').val()) == ''){
		alert('确认密码不能为空！') ;
		res = false ;
		return ;
	}
	if($.trim($('#payPassword').val()) == ''){
		alert('交易密码不能为空！') ;
		res = false ;
		return ;
	}
	if($.trim($('#payPasswordt').val()) == ''){
		alert('确认交易密码不能为空！') ;
		res = false ;
		return ;
	}
	if($.trim($('#password').val()) != $.trim($('#passwordt').val())){
		alert('两次输入登陆密码不相同！') ;
		res = false ;
		return ;
	}
	if($.trim($('#payPassword').val()) != $.trim($('#payPasswordt').val())){
		alert('两次输入交易密码不相同！') ;
		res = false ;
		return ;
	}
	if($.trim($('#email').val()) == ''){
		alert('邮箱不能为空！') ;
		res = false ;
		return ;
	}
	if($.trim($('#traderName').val()) == ''){
		alert('单位名称不能为空！') ;
		res = false ;
		return ;
	}
	
	if($.trim($('#legelName').val()) == ''){
		alert('负责人不能为空！') ;
		res = false ;
		return ;
	}
	if($.trim($('#legelCard').val()) == ''){
		alert('身份证不能为空！') ;
		res = false ;
		return ;
	}
	if($.trim($('#mobile').val()) == ''){
		alert('手机号不能为空！') ;
		res = false ;
		return ;
	}
	if($.trim($('#username').val()) == ''){
		alert('登陆名不能为空！') ;
		res = false ;
		return ;
	}
	if($.trim($("#provinceId").val()) == '0'){
		alert('省份不能为空！') ;
		res = false ;
		return ;
	}
	if($("#cityId").val() == '0'){
		alert('市不能为空！') ;
		res = false ;
		return ;
	}
	if($("#areaId").val() == '0'){
		alert('区不能为空！') ;
		res = false ;
		return ;
	}
	if($.trim($("#address").val()) == ''){
		alert('联系地址不能为空！') ;
		res = false ;
		return ;
	}
	checkUserName() ;
	checkTraderName() ;
	var param = $('#inputForm').serialize() + '&trader.provinceName='+$('#provinceId option:selected').text()+'&trader.cityName='
	+$('#cityId option:selected').text() + '&trader.areaName=' + $('#areaId option:selected').text() ;
	$.post(
		'${web_base}/ekhui/trader!addComplete.htm' ,
		param ,
		function(data){
			if(data == 'true'){
				alert('添加商户成功') ;
			}else if(data == 'false'){
				alert('添加商户失败') ;
			}
			window.location.href = "${web_base}/ekhui/trader!list.htm" ;
		}
	);
}
</script>

<div class="body">
<div class="inputBar">
    <h1><span class="icon">&nbsp;</span><#if isAdd??>添加商户信息<#else>编辑商户信息</#if></h1>
</div>
<div style="display: none;" id="validateErrorContainer" class="validateErrorContainer">
    <div class="validateErrorTitle">以下信息填写有误,请重新填写</div>
    <ul></ul>
</div>
<form id="inputForm" class="validate"
      method="post">
      <ul class="tab">
        <li>
            <input type="button" value="操作员基本信息" hidefocus="true"/>
        </li>
    </ul>
     <table class="inputTable tabContent">
        <tr>
            <th>
                登陆名：
            </th>
            <td>
                   <input type="text" id="username" name="operator.operatorName" onblur="checkUserName()" class="formText {required: true}" value=""/>
                   <label class="requireField">*</label>
            </td>
        </tr>
         <tr>
            <th>
                密 码：
            </th>
            <td>
                    <input type="password" id="password" name="operator.operatorPassword" class="formText {required: true}"  />
                    <label class="requireField">*</label>
            </td>
        </tr>
        <tr>
            <th>
                  确认密码：
            </th>
            <td>
                    <input type="password" id="passwordt" class="formText {required: true}"  />
                    <label class="requireField">*</label>
            </td>
        </tr>  
 		 <tr>
            <th>
               交易密 码：
            </th>
            <td>
                    <input type="password" id="payPassword" name="account.payPassword" class="formText {required: true}"  />
                    <label class="requireField">*</label>
            </td>
        </tr>         
     <tr>
            <th>
              确认交易密码：
            </th>
            <td>
                    <input type="password" id="payPasswordt" class="formText {required: true}"  />
                    <label class="requireField">*</label>
            </td>
        </tr>  
               <tr>
            <th>
        Q Q：
            </th>
            <td>
                   <input type="text" id="number" name="operator.qq" onkeyup="value=value.replace(/[^\d]/g,'') "
          onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" />
            </td>
        </tr>
       <tr>
            <th>
                邮箱：
            </th>
            <td>
                   <input type="text" id="email" name="operator.email" class="formText {required: true}" />
                   <label class="requireField">*</label>
            </td>
        </tr>
     </table>   
    <ul class="tab">
        <li>
            <input type="button" value="商户基本信息" hidefocus="true"/>
        </li>
    </ul>
    <input type="hidden" name="trader.traderId" class="formText" value="${(trader.traderId)!}"/>
    <table class="inputTable tabContent">
        <tr>
            <th>
                单位名称：
            </th>
            <td>
                <#if isAdd??>
                    <input type="text" id="traderName" name="trader.traderName" onblur="checkTraderName()" class="formText {required: true}" 
                          />
                    <label class="requireField">*</label>
                <#else>
                    <input type="text" name="trader.traderName" class="formText {required: true}"
                           value="${(trader.traderName)!}"/>
                </#if>
            </td>
        </tr>
        <tr>
            <th>
                负责人:
            </th>
            <td>
            	<#if isAdd??>
                    <input type="text" id="legelName" name="trader.legelName" class="formText {required: true}"
                          />
                <#else>
                    <input type="text" id="legelName" name="trader.legelName" class="formText {required: true}"
                           value="${(trader.legelName)!}"/>
                </#if>
                
                <label class="requireField">*</label>
            </td>
        </tr>
        <tr>
            <th>
                身份证
            </th>
            <td>
                <#if isAdd??>
                    <input type="text" id="legelCard" name="trader.legelCard" class="formText {required: true}"
                          />
                <#else>
                    <input type="text" id="legelCard" name="trader.legelCard" class="formText {required: true}"
                           value="${trader.legelCard!''}" />
                </#if>
                <label class="requireField">*</label>
                <span id="priceSpan" style="color: red;"></span>
            </td>
        </tr>

        <tr>
            <th>
               手机号码
            </th>
            <td>

                <#if isAdd??>
                    <input type="text" id="mobile" name="trader.mobile" class="formText {required: true}"
                           value="" />
                <#else>

                    <input type="text" id="mobile" name="trader.mobile" class="formText {required: true}"
                           value="${trader.mobile!''}"/>
                </#if>
                <label class="requireField">*</label>
                <span id="priceSpan2" style="color: red;"></span>
            </td>
        </tr>

        <tr>
            <th>
                固定电话：
            </th>
            <td>
             <#if isAdd??>
                <input type="text" name="trader.telephone" class="formText {required: false}"
                     />
             <#else>
             	<input type="text" name="trader.telephone" class="formText {required: false}"
                    value="${trader.telephone!''}" />
             </#if>        
            </td>
        </tr>
        <tr>
            <th>
                省：
            </th>
            <td>
             <select id="provinceId" name="trader.provinceId" onchange="getCitys();">
          		<option value="0">请选择省</option>
          <#if provinceList?exists>		
          	<#list provinceList as province>
          		<option value="${province.provinceId}">${province.provinceName}</option>
          	</#list>
          </#if>	
          </select>
            </td>
        </tr>
        
            <tr>
            <th>
                市：
            </th>
            <td>
            <div id="cityDiv"></div>
            </td>
        </tr>
        
     	<tr>
            <th>
                区/县：
            </th>
            <td>
            <div id="areaDiv"></div>
            </td>
        </tr> 
        
        <tr>
            <th>
                联系地址：
            </th>
            <td>
             <#if isAdd??>
                <input type="text" id="address" name="trader.address" class="formText {required: false}"
                     />
             <#else>
             	<input type="text" id="address" name="trader.address" class="formText {required: false}"
                    value="${trader.address!''}" />
             </#if>  
              <label class="requireField">*</label>      
            </td>
        </tr>
        
        <tr>
            <th>
                商户类型：
            </th>
            <td>
                <#if !isAdd??>
                    <select name="trader.traderType" class="formText {required: true}">
                        <option value="">请选择...</option>
                        <option value="3" <#if trader.traderType=="3">selected="selected"</#if>>
                          	  销售商
                        </option>
                    </select>
                <#else>
                    <select name="trader.traderType" class="formText {required: true}">
                        <option value="3">销售商</option>
                    </select>
                </#if>
                <label class="requireField">*</label>
            </td>
        </tr>
        
    </table>
    <div class="buttonArea">
        <input type="button" class="formButton" <#if isAdd??> value="添  加" onclick="addTrader()"  <#else> value="更  新"</#if>hidefocus="true" />&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="button" class="formButton" onclick="window.history.back(); return false;" value="返  回"
               hidefocus="true"/>
    </div>
</form>
</div>
</body>
</@edit>
